//% color="#ff9f06" iconWidth=50 iconHeight=40
namespace RFID_Module{
    
    //% block="Initialize RFID module [VER]" blockType="command"  
    //% VER.shadow="dropdown" VER.options="VER"
    export function RFID_init(parameter: any, block: any) { 
        let ver=parameter.VER.code; 

        if(Generator.board === 'esp32s3bit'){
            Generator.addImport(`from unihiker_k10 import *`);
        }else if(Generator.board === 'esp32'){
            Generator.addImport(`from mpython import *`);
        }
        // Generator.addImport(`from machine  import Pin`);

        if (ver == "V1"){
            Generator.addImport(`from dfrobot_pn7150 import *`);
            
            if(Generator.board === 'esp32s3bit'){
                Generator.addInit(`RFID_mode`,`RFID = DFRobot_PN7150()`);
            }else if(Generator.board === 'esp32'){
                Generator.addInit(`RFID_mode`,`RFID = DFRobot_PN7150(scl=Pin.P19,sda=Pin.P20)`);
            }

            Generator.addInit(`RFID_connect`,`
while (RFID.connect() == False):
    print("init fail!")
    time.sleep(1)
print("successful connection.")
    `);
            Generator.addInit(`RFID_mode`,`RFID.mode_rw()`);

        }else{
            Generator.addImport('from dfrobot_fm17550 import Rfid');

            if(Generator.board === 'esp32s3bit'){
                Generator.addInit(`RFID_object`,`RFID = Rfid()`);
            }else if(Generator.board === 'esp32'){
                Generator.addInit(`RFID_object`,`RFID = Rfid(scl=Pin.P19,sda=Pin.P20)`);
            }

            Generator.addInit(`RFID_connect`,`RFID.begin()`); 
        }

    }

    //% block="Write [DATA] to byte [BYTE_NUM] of RFID block [BLOCK_NUM]" blockType="command" 
    //% DATA.shadow="range" DATA.params.min=0 DATA.params.max=255 DATA.defl=200
    //% BYTE_NUM.shadow="dropdown" BYTE_NUM.options="BYTE_NUM"
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    export function RFID_write(parameter: any, block: any) { 
        let data=parameter.DATA.code;
        let block_num=parameter.BLOCK_NUM.code;
        let byte_num=parameter.BYTE_NUM.code;

//         Generator.addInit(`RFID_write`,`
// def RFID_write(block,data,types):
//   rf = RFID.scanning(wait=False)
//   if rf is not None:
//     rf.write_block(block, data, types)`);
                    
        // Generator.addCode(`RFID_write(${block_num}, ${data}, ${byte_num})`);
        Generator.addCode(`RFID.write_index_data(${block_num}, ${byte_num}, ${data})`);
    }

    //% block="Read all data from block [BLOCK_NUM] of the RFID module(string)" blockType="reporter" 
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    export function RFID_read_all(parameter: any, block: any) { 
        let block_num=parameter.BLOCK_NUM.code;

//         Generator.addInit(`RFID_read_all`,`
// def RFID_read_all(block):
//   rf = RFID.scanning(wait=False)
//   if rf is not None:
//     return rf.read_block(block)`);        
//         Generator.addCode(`RFID_read_all(${block_num})`);
        Generator.addCode(`RFID.read_data(${block_num})`);

    }

    //% block="Read byte [BYTE_NUM] of RFID block [BLOCK_NUM]" blockType="reporter" 
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    //% BYTE_NUM.shadow="dropdown" BYTE_NUM.options="BYTE_NUM"
    export function RFID_read(parameter: any, block: any) { 
        let block_num=parameter.BLOCK_NUM.code;
        let byte_num=parameter.BYTE_NUM.code;
//         Generator.addInit(`RFID_read`,`
// def RFID_read(block,types):
//     rf = RFID.scanning(wait=False)
//     if rf is not None:
//       return rf.read_block(block,types)`);      
//         Generator.addCode(`RFID_read(${block_num}, ${byte_num})`);
        Generator.addCode(`RFID.read_data(${block_num}, ${byte_num})`);
    }



    //% block="Read the UID of the tag card" blockType="reporter" 
    export function RFID_read_UID(parameter: any, block: any) { 
//         Generator.addInit(`RFID_read_UID`,`
// def RFID_read_UID():
//     rf = RFID.scanning(wait=False)
//     if rf is not None:
//         return rf.read_uid()`); 
//         Generator.addCode(`RFID_read_UID()`);
        Generator.addCode(`RFID.read_uid()`);
    }


    //% block="Detected card. UID:[UID]?" blockType="boolean" 
    //% UID.shadow="string" UID.defl="acee50e3"
    export function RFID_SCAN_UID(parameter: any, block: any) { 
        let uid=parameter.UID.code;
// Generator.addInit(`RFID_read_UID_scan`,`
// def RFID_read_UID_scan(uid):
//     rf = RFID.scanning(wait=False)
//     if rf is not None:
//         if uid == rf.read_uid():
//             return True
//         else:
//             return False`); 
//         Generator.addCode(`RFID_read_UID_scan(${uid})`);
        Generator.addCode(`RFID.scan(${uid})`);
    }


    //% block="Detected card?" blockType="boolean" 
    export function RFID_SCAN(parameter: any, block: any) { 

        // Generator.addCode(`RFID.scanning(wait=False) is not None`);
        // Generator.addCode(`RFID.scanning()`);
        Generator.addCode(`RFID.scan()`);
    }

}

